<?php
/**
 * RadishPHP based controller object.
 *
 * @author Lei Lee
 * @version 1.0
 */
class BaseController {
	/**
	 * Reference Template object.
	 *
	 * @var Template
	 */
	protected $template = NULL;
	
	/**
	 * Reference DbPdo object.
	 *
	 * @var DbPdo
	 */
	protected $db = NULL;
	
	/**
	 * Reference DbTree object.
	 *
	 * @var DbTree
	 */
	protected $dbTree = NULL;
	
	/**
	 * Reference Cache object.
	 *
	 * @var Cache
	 */
	protected $cache = NULL;
	
	/**
	 * Reference WebClient object.
	 *
	 * @var WebClient
	 */
	protected $http = NULL;
	
	/**
	 * Reference Thumbnail object.
	 *
	 * @var Thumbnail
	 */
	protected $thumb = NULL;
	
	/**
	 * Reference FileSystem object.
	 *
	 * @var FileSystem
	 */
	protected $fso = NULL;
	
	/**
	 * Reference SystemData object.
	 *
	 * @var SystemData
	 */
	protected $data = NULL;
	
	/**
	 * Whether the POST request method?
	 *
	 * @var boolean
	 */
	protected $isPost = false;
	
	/**
	 * RadishPHP object.
	 *
	 * @var RadishPHP
	 */
	protected $scope = NULL;
	
	/**
	 * The current system timestamp.
	 *
	 * @var int
	 */
	protected $SystemTime = 0;
	
	/**
	 * Constructor.
	 *
	 */
	function __construct() {
		$this->isPost = (strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') == 0 ? true : false);
		$this->SystemTime = time();
	}
	
	/**
	 * Template object assignment.
	 *
	 * @param string $var
	 * @param mixed $data
	 */
	function assign($var, $data) {
		if ($this->template == NULL) 
			throw new RuntimeException('Template object does not exist or has not been instantiated.', -1);
		
		$this->template->assign($var, $data);
	}
	
	/**
	 * Print the template file.
	 *
	 * @param string $tpl_file
	 * @param boolean $exit
	 * @param array $extra_data
	 */
	function display($tpl_file, $exit = false, $extra_data = NULL) {
		if ($this->template == NULL) 
			throw new RuntimeException('Template object does not exist or has not been instantiated.', -1);
		
		$this->template->assign('sys', array(
			'gets' => &$_GET, 'post' => &$_POST, 'envs' => &$_SERVER
		));
		
		$this->template->display($tpl_file, $exit, $extra_data);
	}
	
	/**
	 * Redirected to the specified module page.
	 * Note: Allowed to pass three parameters, or just pass in a dot-separated string.
	 * -------------------------------------------------------------------------------
	 * eg: 1 => goto($module, $controller, $action)
	 *     2 => goto('$module.$controller.$action');
	 * -------------------------------------------------------------------------------
	 */
	function go() {
		$r = '';
		$args_size = func_num_args();
		
		if (0 == $args_size) {
			header('Location: ' . $this->data->envs('HTTP_REFERER'));
		} else {
			if ($args_size == 3 || $args_size == 2) {
				$args = func_get_args();
				$r = implode('.', $args);
			} elseif ($args_size == 1) {
				$r = func_get_arg(0);
			}
			
			header('Location: ?' . $this->scope->getRouteVar() . '=' . $r);
		}
		exit(0);
	}
	
	/**
	 * Dynamically loaded extension class contains.
	 *
	 * @param string $extraClassName
	 */
	function load($extraClassName) {
		include (RADISHPHP_ROOT_PATH . 'extra' . DIRECTORY_SEPARATOR . $extraClassName . '.class.php');
	}
	
	/**
	 * Set the template object.
	 *
	 * @param Template $obj_tpl
	 * @return BaseController
	 */
	function setTemplate(&$obj_tpl) {
		$this->template = &$obj_tpl;
		return $this;
	}
	
	/**
	 * Set the database handler.
	 *
	 * @param DbPdo $db
	 * @return BaseController
	 */
	function setDbAdapter(&$db) {
		$this->db = &$db;
		return $this;
	}
	
	/**
	 * Set the system data object.
	 *
	 * @param SystemData $data
	 * @return BaseController
	 */
	function setDataAdapter(&$data) {
		$this->data = &$data;
		return $this;
	}
	
	/**
	 * Set the WebClient object.
	 *
	 * @param WebClient $http
	 * @return BaseController
	 */
	function setWebClientAdapter(&$http) {
		$this->http = &$http;
		return $this;
	}
	
	/**
	 * Set the FileSystem object.
	 *
	 * @param FileSystem $fso
	 * @return BaseController
	 */
	function setFileSystem(&$fso) {
	    $this->fso = &$fso;
	    return $this;
	}
	
	/**
	 * Set the Thumbnail object.
	 *
	 * @param Thumbnail $thumb
	 * @return BaseController
	 */
	function setThumbnail(&$thumb) {
		$this->thumb = &$thumb;
		return $this;
	}
	
	/**
	 * Set the RadishPHP instance.
	 *
	 * @param RadishPHP $scope
	 * @return BaseController
	 */
	function setScope(&$scope) {
		$this->scope = &$scope;
		return $this;
	}
	
	/**
	 * Set the DbTree instance.
	 *
	 * @param DbTree $dbTree
	 * @return BaseController
	 */
	function setDbTree(&$dbTree) {
		$this->dbTree = &$dbTree;
		return $this;
	}
	
	/**
	 * Set the Cache instance.
	 *
	 * @param Cache $cache
	 * @return BaseController
	 */
	function setCache(&$cache) {
		$this->cache = &$cache;
		return $this;
	}
	
	/**
	 * Destroy the database connection and other objects.
	 *
	 */
	function destroy() {
		if (!is_null($this->db)) 
			$this->db->close();
	}
}